package com.echo.blog.business.elasticsearch.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.echo.blog.business.blog.model.Blog;
import com.echo.blog.business.blog.service.IBlogService;
import com.echo.blog.business.elasticsearch.dao.BlogRepository;
import com.echo.blog.business.elasticsearch.model.EsBlog;
import com.echo.blog.business.elasticsearch.service.EsBlogService;
import com.echo.model.constant.response.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.completion.Completion;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/esBlog")
public class EsBlogController {

    public static final String TITLE = "title";
    //分页参数
    Integer PAGE_SIZE = 10;

    //默认页码
    Integer DEFAULT_PAGE_NUMBER = 1;

    @Resource
    private IBlogService blogService;

    @Resource
    private EsBlogService esBlogService;

    @Resource
    private BlogRepository blogRepository;


    @DeleteMapping(value = "/deleteAllIndex")
    public ResponseData deleteAllIndex() {
        log.info(">>>>>>即将清空博客索引...");
        blogRepository.deleteAll();
        log.info(">>>>>>博客索引已清空");
        return ResponseData.success(200, "博客索引已清空", null);
    }

    @RequestMapping(value = "/bulkEsBlogIndex")
    public ResponseData bulkEsBlogIndex() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Blog> list = blogService.getAllPublishedBlogs();
        List<EsBlog> esList = new ArrayList<>();
        list.forEach(blog -> {
            List<String> suggestList = new ArrayList<>();
            suggestList.add(blog.getIntro());
            Completion completion = new Completion(suggestList.toArray(new String[suggestList.size()]));
            EsBlog esBlog = new EsBlog(blog.getId().toString(),
                    blog.getTitle(),
                    blog.getIntro(),
                    blog.getCoverUrl(),
                    sdf.format(blog.getCreateTime()),
                    sdf.format(new Date()),
                    completion);
            esList.add(esBlog);
        });
        if (CollectionUtils.isNotEmpty(esList)) {
            log.info(">>>>>>即将初始化博客索引...");
            blogRepository.saveAll(esList);
            log.info(">>>>>>博客索引初始化成功!");
            return ResponseData.success(200, "初始化博客索引到elasticsearch服务器成功", null);
        } else {
            log.error("没有找到博客内容，无法建立任何索引");
            return ResponseData.error("没有找到博客内容，无法建立索引");
        }
    }

    @GetMapping(value = "getEsBlogs")
    public ResponseData getEsBlogs(Integer pageNumber, Integer pageSize, String searchContent) {

        //validate1：  校验分页参数
        if (pageSize == null || pageSize <= 0) {
            pageSize = PAGE_SIZE;
        }

        if (pageNumber == null || pageNumber < DEFAULT_PAGE_NUMBER) {
            pageNumber = DEFAULT_PAGE_NUMBER;
        }

        List<EsBlog> esBlogs = esBlogService.searchEsBlogList(pageNumber, pageSize, searchContent);
        return ResponseData.success(200, "获取es中的博客信息成功", esBlogs);
    }

    @GetMapping(value = "getSuggestEsBlogTitle")
    public ResponseData getEsBlogs(String suggestValue) {
        List<String> suggest = esBlogService.getSuggestList("suggest", suggestValue, 10);
        return ResponseData.success(200, "获取es推荐成功", suggest);
    }


}
